<template>
	<view>
		<view class="vmodul">
			<view class="cu-form-group" style="font-size: 38upx;font-weight: 500;">
				<text class="">生成任务单</text>
			</view>
		<view>
		<view class="cu-form-group">
			<view class="reason">指派人：</view>
			<input class="" disabled v-model="zhipairenName" type="text"  placeholder="请输入" />
		</view>
		<view class="cu-form-group">
			<view class="reason">实施人：</view>
			<picker @change="bindShiShiRenChange" :value="index" :range="shishirenNames" class="picker">
				<view class="picker" style="font-weight: 300;">{{shishirenNames[index]}}</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="reason">类型：</view>
				<picker @change="bindTaskTypeChange" :value="typeIndex" :range="taskTypeNames" class="picker">
					<view class="picker" style="font-weight: 300;">{{taskTypeNames[typeIndex]}}</view>
				</picker>
		</view>
		<view class="cu-form-group">
			<view class="reason">结束时间：</view>
				<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
			        <view class="picker">{{date}}</view>
				</picker>
		</view>
		<view class="cu-form-group">
			<view class="reason">地点：</view>
				<picker @change="placeBindPickerChange" :value="placeIndex" :range="placeNames" style="">
				  <view class="picker">{{placeNames[placeIndex]}}</view>
				</picker>
			</view>
		<view class="cu-form-group">
			<view class="reason">内容：</view>
			<textarea class="" v-model="content" type="text" placeholder="请输入" />
		</view>
		<view class="cu-form-group">
			<view class="reason">备注：</view>
				<input class="" v-model="remarks" type="text" placeholder="请输入" />
			</view>
		<view class="cu-form-group">
			<view>图片凭证</view>
				<view>
					<sunui-upimg @change="getImageInfo" url="http://120.24.62.145:8153/monitor/taskInfo/uploadPicture" :upload_auto="true" ref="upimg"  :upload_count="1"></sunui-upimg>
				</view>
			</view>
		</view>
		</view>
		<view>
			<view style="padding-top: 43upx;padding-bottom: 43upx;">
				<button @click="sureSubmit" style="background-color:#11CC99;height: 80upx;width: 80%;"><span>生成</span></button>
			</view>
		</view>
	</view>
</template>

<script>
	import Util from '@/utils/util.js'
	export default {
		data() {
			const currentDate = this.getDate({
			    format: true
			})
			return {
				title: 'picker',
				shishirenNames: [],
			    shishirenIds: [],
				shishirenTels: [],
				index: 0,
				placeNames:[],
				placeIds:[],
				placeIndex:0,
				zhipairenId:'',
				zhipairenName:'',
				pictureUrl:'',
				content:'',
				remarks:'',
				date: currentDate,
				taskTypeIds:['xunjian','bidui','weixiu','qita'],
				taskTypeNames:['巡检','比对','故障维修','其它'],
				typeIndex:0,
			}
		},
		computed: {
		    startDate() {
		        return this.getDate('start');
		    },
		    endDate() {
		        return this.getDate('end');
		    }
		},
		onShow(){
			this.initParams()
		},
		methods: {
			getImageInfo(e) {
				if(e.length>0&&e[0].length>0){
					this.pictureUrl = e[0][0]
				}
			},
			sureSubmit:function(e){
				var taskInfo={
					"zhipairenId":this.zhipairenId,
					"zhipairenName":this.zhipairenName,
					"shishirenId":this.shishirenIds[this.index],
					"shishirenName":this.shishirenNames[this.index],
					"shishirenTel":this.shishirenTels[this.index],
					"zhidingEndTime":this.date,
					"siteId":this.placeIds[this.placeIndex],
					"siteName":this.placeNames[this.placeIndex],
					"yunweiContent":this.content,
					"yunweiBeizhu":this.remarks,
					"tempPicture":this.pictureUrl,
					"taskType":this.taskTypeIds[this.typeIndex],
					"laiyuan":'person',
					"opStatus":'new',
					}
				//指派任务
				Util.request('/monitor/taskInfo/add', taskInfo, 'POST').then((res) => {
					uni.showModal({
						title: '提示',
						content: '指派成功',
						showCancel: false,
						confirmText: '确定',
						success: res => {
							if (res.confirm) {
								uni.switchTab({
									url: decodeURIComponent('../index')
								})
								// uni.navigateBack({
								// 	delta:1
								// })
							}
						}
					})
					
				})
			},
			bindShiShiRenChange: function(e) {
				this.index = e.target.value
			},
			bindTaskTypeChange: function(e) {
				this.typeIndex = e.target.value
			},
			placeBindPickerChange: function(e) {
			    this.placeIndex = e.target.value
			},
			bindDateChange: function(e) {
			    this.date = e.target.value
			},
			bindTimeChange: function(e) {
				this.time = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
	
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			initParams(){
				//获取当前人员角色、人员id
				let userInfo = Util.getUserInfo();
				this.zhipairenId = userInfo.userId
				this.zhipairenName = userInfo.nickName
				let isZuZhang =false
				userInfo.roles.map(item => {
					if(item.roleId === '1189428519638192129'||item.roleId==='1236948645795397633'){
						isZuZhang =true
					}
				})
				
				//加载人员
				//这里暂时写死，只有主管、组长才加载人员，运维只能个人
				if(isZuZhang){
					Util.request('/monitor/groupUser/byUser/users', {userId:userInfo.userId}, 'GET').then((res) => {
						this.shishirenNames = []
						this.shishirenIds = []
						this.shishirenTels = []
						res.data.map(item => {
							this.shishirenNames.push(item.nickName)
							this.shishirenIds.push(item.userId)
							this.shishirenTels.push(item.mobile)
						})
					})
				}else{
					this.shishirenNames = []
					this.shishirenIds = []
					this.shishirenTels = []
					this.shishirenNames.push(userInfo.nickName)
					this.shishirenIds.push(userInfo.userId)
					this.shishirenTels.push(userInfo.mobile)
				}
				
				//加载站点
				Util.request('/monitor/groupSite/sites/user', {userId:userInfo.userId}, 'GET').then((res) => {
					this.placeIds = []
					this.placeNames = []
					res.data.map(item => {
						this.placeIds.push(item.siteId)
						this.placeNames.push(item.siteName)
					})
				})
			}
		}
	}
</script>
<style lang="scss">
	.main{
		padding-top: 20px;
		background-color: #F3F4FE;
	}
	.main view{
		color:#C1C5C9;
		background-color: #F3F4FE;
	}
	.main input{
		text-align: right;
		background-color: #F3F4FE;
	}
	.vmodul{
		display: flex;
		flex-direction:column;
		margin-left: 22upx;
		margin-right: 22upx;
		margin-top: 22upx;
		background-color: #FCFCFC;
		padding: 10upx 10upx 10upx 10upx;
		border-radius:22upx
	}
	.reason{
		padding: .4rem .5rem;
	}
	.cuIcon-order::before{
		color: white;
		border-radius: 50%;
		background-color: #53C8E7;
		margin-right: 20upx;
	}
	.cuIcon-moneybagfill::before{
		color: #F2D67A;
		margin-right: 20upx;
	}
	.cuIcon-timefill::before{
		color: #8BB2F3;
		margin-right: 20upx;
	}
	.after-cuIcon-fold::after {
		content: "\e6de";
		color:#008800;
		font-weight: 200;
		font-size: 40upx;
	}
	.after-cuIcon-unfold::after {
		content: "\e661";
		color:#008800;
		font-weight: 200;
		font-size: 40upx;
	}
	.row{
		margin-left: 20upx;
		margin-right: 34upx;
		font-family: "cuIcon";
	}
</style>
